package com.cq.smartelebicycle.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class FunctionView extends ImageView implements OnClickListener {

	private int mWidth;
	private int mHeight;
	private float mX;
	private float mY;
	private float scale = 0.5f;
	
	private OnFuncViewClickListener mFuncViewClickListener;

	public enum Area {
		WIFI, SPORT, MUSIC, OBD, BT, OUT
	}

	public FunctionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setOnClickListener(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			mX = event.getX();
			mY = event.getY();
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mFuncViewClickListener != null) {
			mFuncViewClickListener.onFuncViewClick(getArea());
		}
	}

	public void setOnFuncViewClickListener(OnFuncViewClickListener listener) {
		// TODO Auto-generated method stub
		mFuncViewClickListener = listener;
	}
	
	private Area getArea() {

		double distance = Math.sqrt(Math.pow(mX - mWidth / 2, 2)
				+ Math.pow(mY - mHeight / 2, 2));
		if (distance > mWidth / 2) {
			// ponit out of circle
			return Area.OUT;
		}
		if (distance < mWidth / 2 * scale) {
			return Area.BT;
		}
		boolean arg1 = mX > mY;
		boolean arg2 = mX + mY < mWidth;
		if (arg1 && arg2) {
			return Area.WIFI;
		} else if (arg1) {
			return Area.OBD;
		} else if (arg2) {
			return Area.SPORT;
		} else {
			return Area.MUSIC;
		}

	}
	

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		mWidth = getWidth();
		mHeight = getHeight();
	}

	public interface OnFuncViewClickListener {
		
		void onFuncViewClick(Area type);
		
	}
}
